//
//  LoginViewController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 16/11/11.
//  Copyright © 2016年 Reabam. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "ForgotPasswordViewController.h"
#import "SystemResetPwdViewController.h"
#import "TSAlertTool.h"

@interface LoginViewController ()

// 手机图片
@property (weak, nonatomic) IBOutlet UIImageView *phoneImg;
// 账号输入框
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
// 密码输入框
@property (weak, nonatomic) IBOutlet UITextField *passWordField;

// 忘记密码按钮
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;

// 是否记住密码图片
@property (weak, nonatomic) IBOutlet UIImageView *isRememberBtn;

// 密码是否可见
@property (weak, nonatomic) IBOutlet UIButton *passwordEnable;
// 登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
// 自动登录
@property (weak, nonatomic) IBOutlet UIButton *autoLogin;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置UI
    [self serUpUI];
}

-(void)setNoteStr:(NSString *)noteStr {
    _noteStr=noteStr;
    [MBProgressHUD showTextHudSub:noteStr view:self.view];
}
#pragma mark -- 初始化UI
- (void)serUpUI{
    
    // 登录按钮
    _loginBtn.layer.cornerRadius = 4;
    _loginBtn.layer.masksToBounds = YES;
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginBtn setBackgroundColor:CommonColor];
    
    // 是否记住密码按钮
    [self.autoLogin addTarget:self action:@selector(checkButtonClick) forControlEvents:UIControlEventTouchUpInside];
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:@"SAVEACCOUNT"]) {
//        [_isRememberBtn setImage:[UIImage imageNamed:@"xianshi_normal.png"]];
        [_isRememberBtn setImage:[UIImage imageNamed:@"jizhumima_hight"]];
        _phoneTextField.text=[userDefault objectForKey:@"userNameTextField"];
        _passWordField.text=[userDefault objectForKey:@"passwordTextField"];
        self.saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
    } else {
//        [_isRememberBtn setImage:[UIImage imageNamed:@"buxianshi_normal"]];
        [_isRememberBtn setImage:[UIImage imageNamed:@"jizhumima_normal"]];
        _phoneTextField.text=nil;
        _passWordField.text=nil;
        self.saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
    }
    
    [_forgetBtn setTitleColor:CommonColor forState:UIControlStateNormal];
}

// 是否记住密码
-(void)checkButtonClick {
    self.saveAccount = !_saveAccount;
    if (_saveAccount) {
        NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
        [userDefault setBool:_saveAccount forKey:@"SAVEACCOUNT"];
        [userDefault setObject:_passWordField.text forKey:@"passwordTextField"];
        [userDefault setObject:_phoneTextField.text forKey:@"userNameTextField"];
        [userDefault synchronize];
//        [_isRememberBtn setImage:[UIImage imageNamed:@"xianshi_normal.png"]];
        [_isRememberBtn setImage:[UIImage imageNamed:@"jizhumima_hight"]];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passwordTextField"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userNameTextField"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SAVEACCOUNT"];
        [[NSUserDefaults standardUserDefaults] synchronize];
//        [_isRememberBtn setImage:[UIImage imageNamed:@"buxianshi_normal"]];
        [_isRememberBtn setImage:[UIImage imageNamed:@"jizhumima_normal"]];
    }

}


//密码是否可见
- (IBAction)passwordVisible:(UIButton *)sender {
    _passWordField.secureTextEntry=!_passWordField.secureTextEntry;
    if (_passWordField.secureTextEntry) {
        [_passwordEnable setImage:[UIImage imageNamed:@"yanjing_normal.png"] forState:UIControlStateNormal];
    }
    else
        [_passwordEnable setImage:[UIImage imageNamed:@"yanjing_hight.png"] forState:UIControlStateNormal];
}

// 忘记密码
- (IBAction)forgetPassword:(UIButton *)sender {
    ForgotPasswordViewController *forgetVC = [[ForgotPasswordViewController alloc] init];
    forgetVC.titleName = @"找回密码";
    forgetVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:forgetVC animated:YES];

//    SystemResetPwdViewController *generateVC=[[SystemResetPwdViewController alloc] init];
//    generateVC.view.backgroundColor = [UIColor whiteColor];
//    generateVC.ucode=@"123";
//    generateVC.vcode=@"456";
//    [self.navigationController pushViewController:generateVC animated:YES];
}

// 登录按钮
- (IBAction)loginBtn:(UIButton *)sender {
    
    if ([_phoneTextField.text length] <1) {
        
        [TSAlertTool showAlertMessage:@"请输入账号"];
    } else {
        if ([_passWordField.text length] < 1) {
             [TSAlertTool showAlertMessage:@"请输入密码"];
           } else {
               [MBProgressHUD showIndeterminateHud:@"登录中，请稍后..." delay:30 view:self.view];
//               NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//               NSString* jpushRegId = [user objectForKey:@"jpushRegId"];
               NSString *getVersion =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
               NSMutableDictionary *params = [NSMutableDictionary new];
               [params setValue:_phoneTextField.text forKey:@"mobile"];                    // 手机号码
               [params setValue:[SecutiryTools md5:_passWordField.text] forKey:@"loginWord"]; // 密码
//               [params setValue:jpushRegId forKey:@"jpushRegId"];              // 极光推送
               [params setValue:@"mgr" forKey:@"loginType"];                 // 登录类型
               [params setValue:getVersion forKey:@"appVersion"];              // 当前应用版本号
               [params setValue:@"phone_ios" forKey:@"appType"];               // 登录来源设备
               NSLog(@"1-------%@",params);
               [HttpRequest requestData1:@"Common/Login" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                   NSLog(@"login-------%@",severDataDict.dataDict);
                   if (isSuccess) {
                       if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                           [MBProgressHUD hideProgressHUDInternal:self.view];
                           
                           NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                           [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent0"];
                           [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent1"];
                           [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent2"];
                           [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent3"];
                           [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parent4"];
                           
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"groupName" ] forKey:@"groupName"];
//                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"companyId" ] forKey:@"companyId"];
                           
                           
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"fid"] forKey:@"fid"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"tokenId" ] forKey:@"tokenId"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"userCode" ] forKey:@"userCode"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"userName" ] forKey:@"userName"];
//                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"companyName" ] forKey:@"companyName"];
                           [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"userStatus" ]] forKey:@"userStatus"];
                           
                           [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"sex" ]] forKey:@"sex"];
                           [user setObject:[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"groupId" ]] forKey:@"groupId"];
                           
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"serverAddress"] forKey:@"serverAddress"];
                           
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"headImageUrl"] forKey:@"LoginImageheadImageUrl"];
                           
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"funConfigs"] forKey:@"funConfigs"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"funs"] forKey:@"funs"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"menus" ] forKey:@"menus"];
                           
                           [user setObject:_passWordField.text forKey:@"passwordTextField"];
                           [user setObject:_phoneTextField.text forKey:@"userNameTextField"];
                           [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"userType" ] forKey:@"userType"];
                           [user synchronize];
                           
                           [[NSNotificationCenter defaultCenter] postNotificationName:@"changeTokenId" object:nil];
                           
                           AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                           [app setupViewControllers];
                           [UIApplication sharedApplication].keyWindow.rootViewController = app.viewController;
                           
                       } else {
                           [MBProgressHUD hideProgressHUDInternal:self.view];
                           NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                           [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                       }
                   } else {
                       [MBProgressHUD hideProgressHUDInternal:self.view];
                       NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                       [MBProgressHUD showTextHudSub:subStr view:self.view];
                   }
               }];
           }
            
        }
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

@end
